String interning is a crucial memory optimization technique where identical strings are stored only once in memory, allowing JavaScript engines to reduce memory usage, enable lightning-fast equality checks via pointer comparison, and accelerate parsing and compilation.
String interning is a memory optimization where each unique string value is stored in a global table exactly once, and all references to that string point to the same memory location . Modern JavaScript engines like V8 and JavaScriptCore implement string interning for literal strings, identifiers, and other constant strings found in source code . This technique is fundamental to engine performance because strings appear everywhere—property names, variable identifiers, and literal values—and the overhead of storing duplicate copies would be enormous.
Memory Efficiency: Each distinct string value is stored exactly once, eliminating duplicate copies. For codebases with repeated identifiers or literal strings, this can reduce memory consumption by 30-40% . In Hummingbird's HTML renderer, string interning improved style solving performance by ~30% compared to naive string handling .
Ultra-Fast Equality Comparisons: When strings are interned, comparing two strings becomes a single pointer comparison instead of O(n) character-by-character scanning . This is why string comparisons in Chrome (with interning) are much faster than in older Firefox versions without aggressive interning .
Reduced Allocation Pressure: By reusing existing string instances, interning reduces the number of objects that need garbage collection. This is particularly important for short-lived temporary strings that might otherwise flood the young generation .
Improved Cache Locality: Interned strings are often stored in contiguous memory regions, improving CPU cache utilization compared to scattered heap-allocated strings .
The performance impact is substantial. In Java's HotSpot VM, recent optimizations to string interning showed a ~20% increase in throughput when interning the same string repeatedly, and a ~5% increase even for unique strings . JavaScript engines achieve similar gains. However, the technique isn't free—there's a small cost to caching and lookup, so engines are selective about what they intern .
String literals: Hardcoded strings in source code (e.g., 'hello', 'name') are almost always interned .
Identifiers: Variable names, property names, and function names are interned during parsing .
Concatenated strings: Dynamically created strings (e.g., 'player' + i) are generally NOT interned, though engines may have heuristics to intern repeated results .
String objects: new String('foo') creates a String object wrapper, which is NOT interned because objects must maintain identity semantics .
Long strings: Some engines impose length limits on interning to avoid filling the intern table with huge unique strings .
The implementation varies across engines. Boa, an experimental JavaScript engine written in Rust, uses a dedicated boa_interner crate that stores strings in contiguous memory and assigns each unique string a usize symbol . This allows comparisons and storage using small integers rather than heap-allocated strings. In V8, string interning integrates with the parser—during compilation, literal strings are added to an intern table, and subsequent references reuse the same underlying storage .
From a security perspective, interning also matters. In HTML parsing, attribute names are interned and lowercased during compilation, which is why case-sensitive attribute access is tricky—the original casing is discarded in favor of the interned, canonical version . This trade-off prioritizes performance over preserving source formatting.
For JavaScript developers, interning happens transparently—you don't need to manually intern strings like in Java. But understanding it helps explain performance characteristics: property lookups are fast because engine internals treat strings as atoms, and repeated string literals don't bloat memory. The next time you write code with thousands of identical string literals, remember that the engine is likely storing them all in the same memory location thanks to this powerful optimization.